package game.savecameron;

import java.awt.Point;
import game.physics.GameVector;
import game.sprite.MovableSprite;
import game.sprite.Sprite;

public class TarHeel extends MovableSprite {
    private static final int ENEMY_SPEED = 5;
    boolean flipped;
    Sprite myTarget;

    public TarHeel (Point startLocation, String spriteSet, boolean isActive, Sprite target) {
        super(startLocation, spriteSet, isActive);
        myTarget = target;
    }

    @Override
    public void applyFriction () { }

    @Override
    public void update () {
        moveTowardTarget();
    }

    private void moveTowardTarget(){
        look(myTarget.getCenterCoordinate());
        setVelocity();
    }

    private void look (Point centerCoordinate) {
        look(calculateAngle(getCenterCoordinate()));
    }
    
    public double calculateAngle(Point target) {
        return Math.atan2(target.y - getCenterCoordinate().y, target.x - getCenterCoordinate().x);
}

    public void look(double angle) {
        if (angle < -(Math.PI / 2) || angle > Math.PI / 2) {
            if (!flipped) {
                flipped = true;
                imageName = imageName.substring(0, imageName.indexOf("."))
                        + "_flipped"
                        + imageName.substring(imageName.indexOf("."));
                setImage(imageName);
            }
        } else if (flipped) {
            imageName = imageName.replaceFirst("_flipped", "");
            setImage(imageName);
            flipped = false;
        }
        setRotation(angle);
    }

    private void setVelocity() {
        double dx = (Math.cos(getRotation()) * ENEMY_SPEED);
        double dy = (Math.sin(getRotation()) * ENEMY_SPEED);
        this.setVelocity(new GameVector(dx, dy));
    }


}
